package com.zjc.activiti;

import com.zjc.service.FirstService;
import com.zjc.service.HelloService;
import com.zjc.service.SecondService;
import com.zjc.service.ThirdService;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.activiti.engine.task.TaskQuery;
import org.junit.Test;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lv_03575 on 2018/4/9.
 */
public class HelloWord {



    /**
     * 1.部署流程定义
     */
    @Test
    public  void deploymentProcess(){
        ProcessEngine processEngine=ProcessEngines.getDefaultProcessEngine();
        Deployment deployment=processEngine.getRepositoryService()//流程定义和部署对象相关的service
                                            .createDeployment()//创建一个部署对象
                                            .name("zjc")//添加部署的名称
                                            .addClasspathResource("helloword.bpmn")//从classpath中加载资源，一次只能加载一个
//                                            .addClasspathResource("*.png")
                                            .deploy();//完成部署
        System.out.println("部署id:"+deployment.getId());
        System.out.println("部署id:"+deployment.getName());
    }

    /**
     * 2.启动流程定义
     */
    @Test
    public  void startProcess(){
        ProcessEngine processEngine=ProcessEngines.getDefaultProcessEngine();
        Map<String,Object> map=new HashMap<>();
        map.put("first",new FirstService());
        map.put("second",new SecondService());
        map.put("third",new ThirdService());
        ProcessInstance processInstance=processEngine.getRuntimeService()//正在执行的流程实例和执行对象相关的service
                     .startProcessInstanceByKey("hello_process",map);//使用流程定义的key启动流程实例，key对应的*.bpmn文件中id的值,使用key启动，默认按照最新版本启动
        System.out.println("流程实例id:"+processInstance.getId());//流程实例id
        System.out.println(processInstance.getProcessDefinitionId());//流程定义id


    }

    /**
     * 3.查询当前人的个人任务
     */
    @Test
    public  void queryMyTask(){
        ProcessEngine processEngine=ProcessEngines.getDefaultProcessEngine();
        TaskService taskService=processEngine.getTaskService();
        TaskQuery query=taskService.createTaskQuery();
//        query.taskVariableValueEquals()
        List<Task> list=processEngine.getTaskService()//正在执行的任务管理相关的service
                     .createTaskQuery()//创建查询任务对象
                    .taskDefinitionKey("hello_process")
//                     .taskAssignee("张三")//指定办理人
                     .list();
        if(null!=list&&list.size()>0){
            for(Task task:list){
                System.out.println("任务ID"+task.getId());
                System.out.println("任务名称"+task.getName());
                System.out.println("任务创建时间"+task.getCreateTime());
            }
        }
    }

    /**
     * 4.完成自己的任务
     */
    @Test
    public  void completeMyTask(){
        ProcessEngine processEngine=ProcessEngines.getDefaultProcessEngine();
        processEngine.getTaskService().complete("7504");//
    }
}
